package redpacket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: feiwang_6
 * @create: 2020/7/15 16:08
 * @description:
 */
public class LuckyDraw {
    public Integer MAX_RED_NUMBER = 200 * 100;
    public Integer MIN_RED_NUMBER = 1;
    //每次最大红包：每次平均值*MAX_PRO_NUMBER
    public Double MAX_PRO_NUMBER = 2.2;
    
    public List<Integer> skipReadPacket(int total, int count){
        List<Integer> result = new ArrayList<>();
        for(int i = 0; i < count; i++){
            int readPacket = createReadPacket(total, count - i);
            total -= readPacket;
            result.add(readPacket);
        }
        return result;
    }
    
    public int createReadPacket(int total, int count){
        if(count == 1){
            return total;
        }
        int maxRead = MAX_RED_NUMBER < total ? MAX_RED_NUMBER : total;
        int readNumber = (int) (Math.random() * maxRead + MIN_RED_NUMBER);
        boolean checked = checkReadNumber(total, count, readNumber);
        if(!checked){
            return createReadPacket(total, count);
        }
        return readNumber;
    }
    
    public boolean checkReadNumber(int total, int count, int readNumber){
        int maxRead = (total / count) * MIN_RED_NUMBER;
        if(readNumber > maxRead){
            return false;
        }
        
        int nextAvg = (total-readNumber) / (count - 1);
        if(nextAvg > MAX_RED_NUMBER || nextAvg < MIN_RED_NUMBER){
            return false;
        }
        return true;
    }
    
    public static void main(String[] args) {
        LuckyDraw luckyDraw = new LuckyDraw();
        List<Integer> list = luckyDraw.skipReadPacket(100 * 200, 100);
        System.out.println(list);
        System.out.println(list.stream().mapToInt(f -> f).sum());
    }
}
